package studyguide.ch08;

import java.io.IOException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class GaugesMidlet extends MIDlet {

	private final Form form;
	private final Command exitCommand;
	private final Command showAlertCommand;
	private final Display display;
	
	private final Gauge ciGauge;
	private final Gauge crGauge;
	private final Gauge iiGauge;
	private final Gauge iuGauge;
	
	private class SampleCommandListener implements CommandListener {

		public void commandAction(Command command, Displayable displayable) {
			if (command == exitCommand) {
				destroyApp(true);
				notifyDestroyed();
			} else if (command == showAlertCommand) {
				Alert alert = new Alert("Please wait");
				alert.setString("Please wait");
				alert.setTimeout(5 * 1000);
				
				final Gauge indicator = new Gauge(null, false, 5, 0);
				indicator.setValue(1);
				alert.setIndicator(indicator);
				
				// Create a thread to update the indicator
				Thread thread = new Thread(new Runnable () {
					public void run() {
						while (true) {
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {}
							indicator.setValue(1 + indicator.getValue());
						}
					}
					
				});
				thread.start();
				
				display.setCurrent(alert);
			} else {
				throw new RuntimeException("UNKNOWN COMMAND" + command);
			}
		}
		
	}
	
	public GaugesMidlet() throws IOException {
		form = new Form("Form Sample");
		
		exitCommand = new Command("Exit", Command.EXIT, 1);
		showAlertCommand = new Command("Show Alert", Command.OK, 1);
		
		form.addCommand(exitCommand);
		form.addCommand(showAlertCommand);
		SampleCommandListener listener = new SampleCommandListener();
		form.setCommandListener(listener);
		display = Display.getDisplay(this);
		
		// Assemple form:
		ciGauge = new Gauge("CONTINUOUS IDLE", false, Gauge.INDEFINITE, 0);
		crGauge = new Gauge("CONTINUOUS RUNNING", false, Gauge.INDEFINITE, 0);
		iiGauge = new Gauge("INCREMENTAL IDLE", false, Gauge.INDEFINITE, 0);
		iuGauge = new Gauge("INCREMENTAL UPDATING", false, Gauge.INDEFINITE, 0);
		
		form.append(ciGauge);
		form.append(crGauge);
		form.append(iiGauge);
		form.append(iuGauge);
	}
	
	protected void destroyApp(boolean unconditional) {
		// No Op
	}

	protected void pauseApp() {
		// No Op
	}

	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(form);
		
		// Start a thread to keep updating the gauge
		Thread thread = new Thread(new Runnable () {
			public void run() {
				while (true) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {}
					ciGauge.setValue(Gauge.CONTINUOUS_IDLE);
					crGauge.setValue(Gauge.CONTINUOUS_RUNNING);
					iiGauge.setValue(Gauge.INCREMENTAL_IDLE);
					iuGauge.setValue(Gauge.INCREMENTAL_UPDATING);
				}
			}
		});
		thread.start();
	}

}
